var field = new Array(new Array(), new Array());
var operator = new Array("等于","不等于","大于","大于等于","小于","小于等于","包含","不包含","以开始","以结束","为NULL","不为NULL","在范围内","不在范围内","等于多项","不等于多项");
var link = new Array("和","或");
var order = new Array("升序", "降序");
var critiriaIndex = 0;
var critiriaHiddenIndex = 0;
var orderIndex = 0;
var groupIndex = 0;
var selectablequery_jsp_query_form;
function createFieldSelect(name,sf) {
	if (!sf) sf = "";
	var fieldSelect = "<SELECT NAME='"+name+"["+critiriaIndex+"]' id='"+name+"_"+critiriaIndex+"'>";
	for (var i=0; i<field[0].length; i++)
	{		 
		var f = field[0][i];
		var ft = field[1][i];
		var fn = field[2][i];			
		fieldSelect+="<OPTION"+(f==sf?" selected ":" ")+"value=\""+f+"\">"+fn+"</OPTION>";
	}
	fieldSelect+="</SELECT>";
	return fieldSelect;
}
function createOperatorSelect(so) {
	if (!so) so = "";
	var operatorSelect = "<SELECT NAME=\"operator["+critiriaIndex+"]\" id='operator_"+critiriaIndex+"'>";
	for (var i=0; i<operator.length; i++)
	{
		var f = operator[i];
		operatorSelect+="<OPTION"+(i==so?" selected ":" ")+"value=\""+i+"\">"+f+"</OPTION>"
	}
	operatorSelect+="</SELECT>";
	return operatorSelect;
}
function createRelationSelect(sr) {
	if (!sr) sr = "";
	var relationSelect = "<SELECT NAME=\"relation["+critiriaIndex+"]\"  id='relation_"+critiriaIndex+"'>";
	for (var i=0; i<link.length; i++)
	{
		var f = link[i];
		relationSelect+="<OPTION"+(i==sr?" selected ":" ")+"value=\""+i+"\">"+f+"</OPTION>"
	}
	relationSelect+="</SELECT>";
	return relationSelect;
}
function createOrderSelect(so) {
	if (!so) so = "";
	var orderSelect = "<SELECT NAME=\"order["+critiriaIndex+"]\">";
	for (var i=0; i<order.length; i++)
	{
		var o = order[i];
		orderSelect+="<OPTION"+(i==so?" selected ":" ")+"value=\""+i+"\">"+o+"</OPTION>"
	}
	orderSelect+="</SELECT>";
	return orderSelect;
}
function createHiddenQueryBar(objId,f,ft,fv,o,r) {
	var obj = $('#'+objId);
	if (!fv) fv = ""; 
	var div="<div>条件:<input type=\"checkbox\" checked id=\"critiriaHiddenIndex\" name=\"critiriaIndex["+critiriaIndex+"]\" value=\""+critiriaHiddenIndex+"\">&nbsp;"+createFieldSelect('critiriaField',f)+"&nbsp;"+createOperatorSelect(o)+"&nbsp;<input type=\"text\" id=\"critiriaHiddenFvalue_"+critiriaHiddenIndex+"\" name=\"critiriaFvalue["+critiriaIndex+"]\" value=\""+fv+"\" size=\"8\"\">&nbsp;"+createRelationSelect(r)+"</div>";
	obj.append(div);
	critiriaHiddenIndex++;
}
function createQueryBar(objId,f,ft,fv,o,r) {
	var obj = $('#'+objId);
	if (!fv) fv = ""; 
	var div="<div>条件:<input type=\"checkbox\" checked id=\"critiriaIndex\" name=\"critiriaIndex["+critiriaIndex+"]\" value=\""+critiriaIndex+"\">&nbsp;"+createFieldSelect('critiriaField',f)+"&nbsp;"+createOperatorSelect(o)+"&nbsp;<input type=\"text\" id=\"critiriaFvalue_"+critiriaIndex+"\" name=\"critiriaFvalue["+critiriaIndex+"]\" value=\""+fv+"\" size=\"8\" onfocus=\"showFieldValueCandicate('critiriaField_"+critiriaIndex+"','critiriaFvalue_"+critiriaIndex+"');\">&nbsp;"+createRelationSelect(r)+"</div>";
	obj.append(div);
	critiriaIndex++;
}
function createGroupBar(objId,f) {
	var obj = $('#'+objId);
	var div="<div>分组:<input type=\"text\" style=\"width:0px;visibility:hidden;\"><input type=\"checkbox\" checked id=\"groupIndex\" name=\"groupIndex["+critiriaIndex+"]\" value=\""+groupIndex+"\">&nbsp;"+createFieldSelect('groupField',f)+"</div>";
	obj.append(div);
	groupIndex++;
}
function createOrderBar(objId,f,o) {
	var obj = $('#'+objId);
	var div="<div>排序:<input type=\"text\" style=\"width:0px;visibility:hidden;\"><input type=\"checkbox\" checked id=\"orderIndex\" name=\"orderIndex["+critiriaIndex+"]\" value=\""+orderIndex+"\">&nbsp;"+createFieldSelect('orderField',f)+"&nbsp;"+createOrderSelect(o)+"</div>";
	obj.append(div);
	orderIndex++;
}
function query(formId, forPage) {
	var valid = false;
	var hasCritiria = false;
	forPage = true;
	var form = $('#' + formId);
	// alert(form.attr("id"));

	// check group parameter, 必须在query参数查询前进行
	//alert($("#queryForm input[id=groupIndex]:checked").length);
	if ($("#queryForm input[id=groupIndex]:checked").length > 0)
	{
		if (!hasCritiria) hasCritiria = true;
		valid = true;
	}

	// check order parameter, 必须在query参数查询前进行
	//alert($("#queryForm input[id=orderIndex]:checked").length);
	if ($("#queryForm input[id=orderIndex]:checked").length > 0)
	{
		if (!hasCritiria) hasCritiria = true;
		valid = true;
	}

	// check query parameter
	//alert($("#queryForm input[id=critiriaIndex]:checked").length);
	$("#queryForm input[id=critiriaIndex]:checked").each(function() {
		if (!hasCritiria) hasCritiria = true;
		var ft = $('#critiriaField_'+$(this).val()).val();
		ft = ft.substr(ft.indexOf(":")+1);
		var fv = $('#critiriaFvalue_'+$(this).val()).val();
		var op = operator[$('#operator_'+$(this).val()).val()];
		valid = validParameter($('#critiriaFvalue_'+$(this).val()),ft,fv,op);
		if (!valid) return false;
	});
	
	//alert("valid=" + valid);
	//alert("hasCritiria=" + hasCritiria);
	if (!forPage && !hasCritiria) {
		alert("请添加查询条件");
	}
	if (hasCritiria && !valid) {
		return false;
	}
	if (forPage || valid) {
		form.submit();
	}	
}
function validParameter(obj,ft,fv,op) {
	//alert(ft+":"+fv+":"+op);
	if (op == "在范围内" || op == "不在范围内") {
		if (!isRange(fv)) {
			alert("必须输入范围, 格式:xx,xx");
			obj.focus();
			obj.select();
			return false;
		}
	} else if (op == "等于多项" || op == "不等于多项") {
	} else {
		if (ft == "int" || ft == "short" || ft == "long" || ft == "byte") {
			if (!isInt(fv)) {
				alert("必须填入整数");
				obj.focus();
				obj.select();
				return false;
			}
		}
		if (ft == "float" || ft == "double") {
			if (!isFloat(fv)) {
				alert("必须填入数字");
				obj.focus();
				obj.select();
				return false;
			}
		}
		if (ft == "date") {
			if (!isDate(fv) && !isDateReplace(fv)) {
				alert("必须输入日期!!格式:yyyy-mm-dd");
				obj.focus();
				obj.select();
				return false;
			}
		}
		if (ft == "string") {
			if (!fv || fv == "") {
				alert("必须输字符串");
				obj.focus();
				obj.select();
				return false;
			}
		}
	}
	
	return true;
}
function isInt(i) {
	var regex = /^[0-9]+$/;
	return regex.test(i);
}
function isFloat(i) {
	var regex = /^[0-9\.]+$/;
	return regex.test(i);
}
function isRange(r) {
	var regex = /^[^,]+,[^,]+$/;
	return regex.test(r);
}
function isDate(date) {
	var regex = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
	return regex.test(date);
}
function isDateReplace(date) {
	var regex = /^\{today\}[+-]{0,1}[0-9]{0,}$/;
	return regex.test(date);
}
function show(objId) {
	var obj = $('#'+objId);
	alert(obj.html());
}
function showFieldValueCandicate(selectId, targetId) {
	// do nothing, just for override
}

// 自定义查询
function selectablequery_jsp_callback(id, action) {
	var select = $("#selectablequery_jsp_query_id");
	if (action == "add" && id > 0) {
		var text = $('#'+"newQueryName").value;
		if (text) {} else { text = "新增查询";}
		var option ="<option value=\""+id+"\" selected>text</option>";
		select.append(option);
		if (selectablequery_jsp_query_form) {
			selectablequery_jsp_query_form.selectablequery_jsp_comeFrom.value="select";
			selectablequery_jsp_query_form.submit();
		} else {
			hideDefine();
		}
	} else if (action == "update" && id > 0) {
		var text = $('#'+"newQueryName").value;
		if (text) {} else { text = "新增查询";}
		$("#selectablequery_jsp_query_id option[value='"+id+"']").text(text);
//		for (i=0; i< select.options.length; i++) {
//			if (select.options(i).value==id) {
//				select.options(i).text = text;
//				break;
//			}
//		}
		if (selectablequery_jsp_query_form) {
			selectablequery_jsp_query_form.selectablequery_jsp_comeFrom.value="select";
			selectablequery_jsp_query_form.submit();
		} else {
			hideDefine();
		}
	} else if (action == "delete") {
		$("#selectablequery_jsp_query_id option[value='"+id+"']").remove();
//		for (i=0; i< select.options.length; i++) {
//			if (select.options(i).value==id) {
//				select.remove(i);
//				break;
//			}
//		}
		hideDefine();
	}
}
function showDefine(form) {
	if (form) {
		selectablequery_jsp_query_form = form;
	}

	$('#'+"queryCritiriaDiv").html($('#'+"queryCritiria").html().replace(/onfocus="[^"]*"/g,""));
	$('#'+"orderCritiriaDiv").html($('#'+"orderCritiria").html());
	$('#'+"groupCritiriaDiv").html($('#'+"groupCritiria").html());

	var screenHeight = document.body.scrollHeight > document.body.clientHeight ? document.body.scrollHeight : document.body.clientHeight;
	// alert(document.body.scrollHeight+":"+document.body.clientHeight+":"+screenHeight);
	var coverIframe =$('#'+"defineCoverIframeDiv");
	coverIframe.css('left','0px');
	coverIframe.css('top','0px');
	coverIframe.css('width',document.body.scrollWidth + 'px');
	coverIframe.css('height',screenHeight + 'px');
	coverIframe.css('visibility','visible');
	coverIframe.css('z-index','997');
	var coverDiv =$('#'+"defineCoverDiv");
	coverDiv.css('left','0px');
	coverDiv.css('top','0px');
	coverDiv.css('width',document.body.scrollWidth + 'px');
	coverDiv.css('height',screenHeight + 'px');
	coverDiv.css('visibility','visible');
	coverDiv.css('z-index','998');
	var divWidth = 550;
	var hiddenDiv =$('#'+"defineHiddenDiv");
	var leftPosition = (document.body.clientWidth - divWidth)/2;
	var heightDiv = hiddenDiv[0].scrollHeight + "px";
	hiddenDiv.css('left',leftPosition + 'px');
	hiddenDiv.css('top','30px');
	hiddenDiv.css('width',divWidth + 'px');
	hiddenDiv.css('height',heightDiv);
	hiddenDiv.css('visibility','visible');
	hiddenDiv.css('z-index','999');
}
function setDefineDialogHeight() {
	var hiddenDiv = $('#'+"defineHiddenDiv")[0];
	var heightDiv = hiddenDiv.scrollHeight + "px";
	hiddenDiv.style.height = heightDiv;
}
function hideDefine() {
	var coverIframe = $('#'+"defineCoverIframeDiv");
	coverIframe.css('visibility','hidden');
	coverIframe.css('z-index','-997');

	var coverDiv = $('#'+"defineCoverDiv");
	coverDiv.css('visibility','hidden');
	coverIframe.css('z-index','-998');

	var hiddenDiv = $('#'+"defineHiddenDiv");
	hiddenDiv.css('visibility','hidden');
	coverIframe.css('z-index','-999');
}

function submitQueryForm(value,formid,page){
	$('#'+page).val(value);
	query(formid, true)
}

function submitQueryFormGoPage(formid,page,gopage){
	var value = $('#'+gopage).val();
	var regex = /^-?\d+$/;
	if(!regex.test(value)){
		alert('必须为数字!');
		return ;
	}
	$('#'+page).val(value);
	query(formid, true);
}

function createXlssay(){
	$('#createXls').val("yes");
	query('queryForm', true);
}